<template>
  <el-row>
    <borrower-detail ref="borrowerDetail"></borrower-detail>
    <el-form :inline="true">
      <el-form-item label="关键字">
        <el-input v-model="search.keyword" placeholder="姓名/手机/身份证" />
      </el-form-item>
      <el-button type="primary" icon="el-icon-search" @click="searchBorrower()">
        查询
      </el-button>
      <el-button type="default" @click="resetData()"> 清空 </el-button>
    </el-form>
    <el-table :data="borrowerList" border style="width: 100%">
      <el-table-column prop="id" label="序号" width="100"> </el-table-column>
      <el-table-column prop="name" label="姓名" width="100"> </el-table-column>
      <el-table-column prop="mobile" label="手机号" width="180">
      </el-table-column>
      <el-table-column prop="idCard" label="身份证号" width="180">
      </el-table-column>
      <el-table-column label="性别" width="100">
        <template slot-scope="scope">
          {{ scope.row.sex === 1 ? "男" : "女" }}
        </template>
      </el-table-column>
      <el-table-column prop="age" label="年龄"> </el-table-column>
      <el-table-column prop="isMarry" label="是否结婚">
        <template slot-scope="scope">
          {{ scope.row.isMarry === 1 ? "是" : "否" }}
        </template>
      </el-table-column>
      <el-table-column prop="integralEnd" label="状态">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.status === 0" type="info" size="mini">
            未认证
          </el-tag>
          <el-tag v-if="scope.row.status === 1" type="warning" size="mini">
            认证中
          </el-tag>
          <el-tag v-if="scope.row.status === 2" type="success" size="mini">
            认证通过
          </el-tag>
          <el-tag v-if="scope.row.status === -1" type="danger" size="mini">
            认证失败
          </el-tag>
        </template>
      </el-table-column>
      <!-- <el-table-column prop="createTime" label="申请时间"> </el-table-column> -->
      <el-table-column label="申请时间" width="100">
        <template slot-scope="scope">
          {{ scope.row.createTime | formatDate }}
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button
            @click="audit(scope.$index, scope.row)"
            v-if="scope.row.status === 1"
            type="warning"
            size="mini"
          >
            审批
          </el-button>
          <el-button
            @click="saveBorrower(scope.$index, scope.row)"
            v-else
            type="primary"
            size="mini"
          >
            查看
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-row>
      <el-col :span="24">
        <el-pagination
          background
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="1"
          :page-sizes="[5, 10, 20]"
          :page-size="5"
          layout="total, sizes, prev, pager, next, jumper"
          :total="borrowerTotal"
        >
        </el-pagination>
      </el-col>
    </el-row>
  </el-row>
</template>

<script>
import { listBorrower, saveBorrower } from "@/api/borrower";
import borrowerDetail from "../../components/borrower/borrowerDetail";
import { formatDate } from "@/utils/date"; //转换日期格式
export default {
  components: { borrowerDetail },
  data() {
    return {
      borrowerList: [],
      borrowerTotal: 0,
      search: {
        keyword: "",
        pageNum: 0,
        limit: 5,
        currentPage: 1,
      },
    };
  },
  methods: {
    searchBorrower() {
      listBorrower(this.search)
        .then((response) => {
          this.borrowerList = response.data.list;
          this.borrowerTotal = response.data.total;
        })
        .catch((error) => {
          this.outputError(error);
        });
    },
    async audit(index, row) {
      try {
        this.$refs.borrowerDetail.$emit("openborrowerDetailDialog", row);
      } catch (error) {}
    },
    // 保存borrower到ServletContext中
    saveBorrower(index, row) {
      saveBorrower(row).then((response) => {
        this.$router.push("/borrowerDetail");
      });
    },
    handleSizeChange(val) {
      this.search.limit = val;
      this.searchBorrower();
    },
    handleCurrentChange(val) {
      this.search.pageNum = val;
      this.search.currentPage = val;
      this.searchBorrower();
    },
    resetData() {
      this.search.keyword = "";
    },
  },
  created() {
    listBorrower(this.search)
      .then((response) => {
        this.borrowerTotal = response.data.total;
        this.borrowerList = response.data.list;
      })
      .catch((error) => {
        console.log(error);
      });
  },
  filters: {
    formatDate(time) {
      var date = new Date(time);
      return formatDate(date, "yyyy-MM-dd");
    },
  },
  components: { borrowerDetail },
};
</script>

<style scoped>
</style>

